<?php

namespace app\kernel\service;

use app\kernel\crontab\CrontabProcess;
use app\kernel\Process;
use app\kernel\queue\amqp\AbstractConsumer;
use app\kernel\queue\amqp\AmqpConsumerProcess;
use PhpAmqpLib\Message\AMQPMessage;

/**
 *  rabbmq 队列
 */
class AmqpService extends Service {

    protected $install = [];

    /**
     * @return void
     */
    public function register () {

        $queues = $this->app->config->get('amqp_queue');
        if (empty($queues)) {
            return;
        }
        foreach ($queues as $queue) {

            try {
                if (!isset($queue['queue']) || !class_exists($queue['queue'])) {
                    continue;
                }
                $consumer = new $queue['queue']();
                if (!$consumer instanceof AbstractConsumer || isset($this->install[$queue['queue']])) {
                    continue;
                }

                $amqp = new AmqpConsumerProcess($this->app);
                $workNum = isset($queue['process_num']) && $queue['process_num'] > 0 ? $queue['process_num'] : 1;
                $worName = !empty($queue['name']) ? $queue['name'] : $consumer->getQueue();

                $amqp->setWorkerNum($workNum); //进场数量
                $amqp->setWorkerName($worName); // 进程名称
                $amqp->load($consumer); //加载队列信息
                $this->install[$queue['queue']] = $consumer;
            } catch (\Exception $exception) {
                $this->app->log->error((string)$exception);
            }
        }
    }

}